package org.toycode.logindemo.application.tapestry.pages;

import java.util.List;
import org.apache.tapestry5.Asset;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.MarkupWriterFactory;
import org.apache.tapestry5.services.ResponseRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.toycode.logindemo.application.mock.ClientMockDataGenerator;
import org.toycode.logindemo.application.tapestry.util.DataModelTransformer;
import org.toycode.logindemo.application.tapestry.util.JSONStreamResponse;
import org.toycode.logindemo.entity.Article;
import org.toycode.logindemo.entity.Paper;
import org.toycode.logindemo.entity.Task;

/**
 * This class provides data table display. 
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.1, Sep 1, 2009
 */
@IncludeStylesheet("context:style/datatable.css")
public class DataTable {

    /**
     * logger.
     */
    private Logger logger = LoggerFactory.getLogger(DataTable.class);
    /**
     * datatable.js library.
     */
    @Inject
    @Path("datatable.js")
    private Asset dataTableLibrary;
    /**
     * render support.
     */
    @Environmental
    private RenderSupport renderSupport;
    /**
     * markup writer factory.
     */
    @Inject
    private MarkupWriterFactory factory;
    /**
     * response renderer. 
     */
    @Inject
    private ResponseRenderer responseRenderer;

    void setupRender() {
        renderSupport.addScriptLink(dataTableLibrary);
    }

    StreamResponse onActionFromPapersLink(final int start, final int rows) {
        logger.debug("Invoking papers link handler method....");

        final List<Paper> papers =
                ClientMockDataGenerator.getInstance().getPapers(start, rows);

        // DataModelTransformer.papers2JSONArray(papers);

        final JSONArray jsonArray = DataModelTransformer.transform2JSONArray(
                papers);
        final JSONObject ret = new JSONObject();
        final int length = ClientMockDataGenerator.getInstance().getPapersSize();
        wrap(ret, jsonArray, start, length);

        return new JSONStreamResponse(ret.toString());
    }

    StreamResponse onActionFromTasksLink(final int start, final int rows) {
        logger.debug("Invoking tasks link handler method....");

        final List<Task> tasks =
                ClientMockDataGenerator.getInstance().getTasks(start, rows);
        final JSONArray jsonArray =
                DataModelTransformer.transform2JSONArray(tasks);
        final JSONObject ret = new JSONObject();
        final int length = ClientMockDataGenerator.getInstance().getTasksSize();
        wrap(ret, jsonArray, start, length);

        return new JSONStreamResponse(ret.toString());
    }

    StreamResponse onActionFromArticlesLink(final int start, final int rows) {
        logger.debug("Invoking articles link handler method....");

        final List<Article> articles =
                ClientMockDataGenerator.getInstance().getArticles(start, rows);
        final JSONArray jsonArray =
                DataModelTransformer.transform2JSONArray(articles);
        final JSONObject ret = new JSONObject();
        final int length =
                ClientMockDataGenerator.getInstance().getArticlesSize();
        wrap(ret, jsonArray, start, length);

        return new JSONStreamResponse(ret.toString());
    }

    private void wrap(final JSONObject ret, final JSONArray jsonArray,
                      final int start, final int length) {
        ret.put("content", jsonArray);
        ret.put("start", start);
        ret.put("totalLength", length);
    }
}
